package top.ochiamalu.aioj.oauth.handler;

import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import top.ochiamalu.aioj.constant.RedirectConstants;
import top.ochiamalu.aioj.domain.vo.user.UserLoginVO;
import top.ochiamalu.aioj.oauth.OAuthCallbackHandler;
import top.ochiamalu.aioj.properties.OAuthProperties;
import top.ochiamalu.aioj.service.OAuthService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * github登录处理程序
 *
 * @author ochiamalu
 * @date 2025/03/12
 */
@Component("githubLoginHandler")
@RequiredArgsConstructor
public class GithubLoginHandler implements OAuthCallbackHandler {

    @Resource
    private OAuthService oAuthService;

    @Resource
    private OAuthProperties oAuthProperties;

    @Override
    public void handle(String code, HttpServletResponse response) throws IOException {
        UserLoginVO userLoginVO = oAuthService.authGithub(code);
        if (userLoginVO != null) {
            StpUtil.login(userLoginVO.getId());
            response.sendRedirect(oAuthProperties.getFrontendUrl() + RedirectConstants.LOGIN_SUCCESS_URL);
            return;
        }
        response.sendRedirect(oAuthProperties.getFrontendUrl() + RedirectConstants.LOGIN_NOT_BIND_ERROR_URL);
    }
} 